﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using System.Data;
using Telerik.Web.UI;

namespace HCFramework.User.Role
{
    public partial class RoleManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // An cac control chua dung den
                PanelAddAndEdit.Visible = false;
                PanelGrid.Visible = true;
            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Roles
                         select a;
            DataTable dt = M.Common.LINQToDataTable(result);
            dt.Columns.Add("Sua");
            dt.Columns.Add("Xoa");
            foreach (DataRow row in dt.Rows)
            {
                if (C.RoleController.CanEdit(row["Name"].ToString()))
                {
                    row["Sua"] = "Edit";
                    row["Xoa"] = "Delete";
                }
                else
                {
                    row["Sua"] = "";
                    row["Xoa"] = "";
                }
                row.AcceptChanges();
            }
            RadGridMain.DataSource = dt;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = true;
                ButtonUpdate.Visible = false;
            }
            if (e.CommandName == "Xoa")
            {
                GridDataItem item = (GridDataItem)e.Item;
                C.RoleController.Delete(int.Parse(item["RoleID"].Text));
            }
            if (e.CommandName == "Sua")
            {
                GridDataItem item = (GridDataItem)e.Item;
                LabelID.Text = item["RoleID"].Text;
                if (!C.RoleController.CanEdit(int.Parse(LabelID.Text))) return;
                BindEditForm();

                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = false;
                ButtonUpdate.Visible = true;
            }
        }
        private void BindEditForm()
        {
            int id = int.Parse(LabelID.Text);
            API.LINQ.Role r = C.RoleController.GetByID(id);
            TextBoxName.Text = r.Name;
        }
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            // Insert a record
            API.LINQ.Role r = new API.LINQ.Role();
            r.Name = TextBoxName.Text;
            C.RoleController.Insert(r);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            // Update
            int id = int.Parse(LabelID.Text);
            HCDataContext db = new HCDataContext();
            API.LINQ.Role r = C.RoleController.GetByID(id, db);
            r.Name = TextBoxName.Text;
            C.RoleController.Update(r, db);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
    }
}